package com.hospital.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hospital.model.UserInfo;
import com.hospital.util.DbUtil;
@WebServlet("/manage/PatientInhospitableServlet")
public class PatientInhospitableServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String method = request.getParameter("method");
		if ("add".equals(method)) {
			String patientId = request.getParameter("patientId").trim();
			String nurse_account = request.getParameter("nurse_name").trim();
			String sickbedId = request.getParameter("sickbedid").trim();
			String deptname = request.getParameter("deptname").trim();
			String state = request.getParameter("state").trim();
			String salemoney = request.getParameter("salemoney").trim();
			String sql = "update t_sickedbed set patientId = '" + patientId + "', deptname = '" + deptname + "', state = '" + state + "',salemoney ='" + salemoney + "' where sickbedId = " + sickbedId;
			DbUtil.add_modify(sql);
			String sqlid = "update t_patient_info set sickbedId = '" + sickbedId + "', intime = '" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()) + "' where id = " + patientId;
			DbUtil.add_modify(sqlid);
			String sqlrecord = "insert into t_inhospital_time_record(patientId , sickbedId, intime, nurse, outtime) values('"			
					+ patientId +"', '" + sickbedId + "', '" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()) + "','" + nurse_account +"','" + "尚未出院" +"')";
			DbUtil.add_modify(sqlrecord);
			request.getRequestDispatcher("/manage/sickbed_info_manage.jsp").forward(request, response);
		} else if("del".equals(method)){
			String sickbedId = request.getParameter("sickbedid").trim();
			String state = "尚未使用";
			String sql = "update t_sickedbed set patientId = '" + "null" + "', state = '" + state + "' where sickbedId = " + sickbedId;
			DbUtil.add_modify(sql);
			String sqlpatient = "update t_patient_info set outtime = '" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()) + "', Inhospitable = '" + null + "',sickbedId ='" + null + "' where sickbedId = " + sickbedId;
			DbUtil.add_modify(sqlpatient);
			String sqlouttime = "update t_inhospital_time_record set outtime = '" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()) + "' where sickbedId = " + sickbedId;
			DbUtil.add_modify(sqlouttime);
			request.getRequestDispatcher("/manage/sickbed_info_manage.jsp").forward(request, response);
		}	
	}	
}